#include "data_structure.h"

// find if there is a cycle in a link list
const bool LinkList_has_cycle(LinkList head) {
	if (nullptr == head->next) return false;

	LNode *p1, *p2;
	p1 = p2 = head;

	// p1 step one step, p2 step two steps once
	while (nullptr != p2->next) {
		p2 = p2->next;
		if (nullptr == p2->next) return false;

		p2 = p2->next;
		p1 = p1->next;

		if (p1 == p2) return true;

	}
	return false;
}